// Copyright (c) 2011 Blue Onion Software, All rights reserved
using System;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;

namespace BlueOnionSoftware.Bloget
{
    public class ArchiveDate : IComparable<ArchiveDate>
    {
        private DateTime _date;

        public ArchiveDate(DateTime dateArg, int countArg)
        {
            _date = dateArg;
            Count = countArg;
        }

        public DateTime Date
        {
            get { return _date; }
            set { _date = value; }
        }

        public int Count { get; set; }

        public int CompareTo(ArchiveDate other)
        {
            return DateTime.Compare(_date, other._date);
        }

        public override int GetHashCode()
        {
            return _date.GetHashCode();
        }

        [SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
        public override bool Equals(object obj)
        {
            if (!(obj is ArchiveDate))
            {
                return false;
            }

            return (CompareTo((ArchiveDate) obj) == 0);
        }

        public static bool operator ==(ArchiveDate left, ArchiveDate right)
        {
            return left.Equals(right);
        }

        public static bool operator !=(ArchiveDate left, ArchiveDate right)
        {
            return !(left == right);
        }

        public static bool operator >(ArchiveDate left, ArchiveDate right)
        {
            return left.CompareTo(right) > 0;
        }

        public static bool operator <(ArchiveDate left, ArchiveDate right)
        {
            return left.CompareTo(right) < 0;
        }
    }

    public class ArchiveDateCollection : KeyedCollection<DateTime, ArchiveDate>
    {
        protected override DateTime GetKeyForItem(ArchiveDate item)
        {
            return item.Date;
        }

        internal ArchiveDate[] ToArray()
        {
            var dates = new ArchiveDate[Count];

            for (var i = 0; i < Count; ++i)
            {
                dates[i] = this[i];
            }

            return dates;
        }
    }
}